package controller;

import dao.Users;
import server.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "logonServlet",urlPatterns = "/logonServlet")
public class logonServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String phone=request.getParameter("phone");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String yzm=request.getParameter("yzm");
        String dxyzm=request.getParameter("dxyzm");
        Users users=new Users();
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        int phones=users.Oneselectphone(phone);

        int usernames=users.Oneselectusername(username);

        PrintWriter out = response.getWriter();

        if(phones==0){
            if(usernames==0){
                int ins=users.insertUser(new User(phone,username,password,sdf.format(date)));
                if(ins>0){
                    out.write("true");
                }else{
                    out.write("false");
                }
            }else{
                out.write("该名字已被使用！");
            }
        }else{
            out.write("该手机号已被注册！");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
